//
// Created by vcq on 21/4/2.
//
#include "model/flow_now.hpp"
#include "model/_user_manager.hpp"

static void GetFlowNow(request &req, response &res) {
    if (!check_login(req, res)) {
        return;
    }
    basic_string_view<char> id = req.get_query_value("id");
    basic_string_view<char> plan = req.get_query_value("plan");
    basic_string_view<char> plan_id = req.get_query_value("plan_id");
    basic_string_view<char> pass = req.get_query_value("pass");
    basic_string_view<char> user_id = req.get_query_value("user_id");
    string tmp;
    if (user_id == "1" || user_id == "-1") {
        if (user_id == "1") {
            pass = "0";
        }
        tmp = to_string(get_user_id(req, res));
    }
    json item = MGetFlowNow(id, plan, plan_id, pass, tmp.data());
    res.set_status_and_content(status_type::ok, item.dump(), req_content_type::json);
}

static void StartFlow(request &req, response &res) {
    if (!check_login(req, res)) {
        return;
    }
    basic_string_view<char> plan = req.get_query_value("plan");
    basic_string_view<char> plan_id = req.get_query_value("plan_id");
    json item = MGetFlow("", plan, plan_id);
    if (item["count"] > 0) {
        for (auto i : item["data"]) {
            struct flow_now flow_now1{};
            flow_now1.plan = atoi(plan.data());
            flow_now1.plan_id = atoi(plan_id.data());
            if (i["type"] == "user") {
                flow_now1.user_id = i["user_id"];
            } else if (i["type"] == "school" || i["type"] == "profession" || i["type"] == "plan" ||
                       i["type"] == "syllabus") {
                auto tmp = MGetUserManager("", "", i["user_id"], i["type"]);
                flow_now1.user_id = tmp["data"]["user_id"];
            }
            flow_now1.pass = 0;
            flow_now1.note = "";
            MAddFlowNow(flow_now1);
        }
    }
    res.set_status_and_content(status_type::ok);
}

static void CancelFlow(request &req, response &res) {
    if (!check_login(req, res)) {
        return;
    }
    basic_string_view<char> plan = req.get_query_value("plan");
    basic_string_view<char> plan_id = req.get_query_value("plan_id");
    json item = MGetFlowNow("", plan, plan_id, "", "");
    if (item["count"] > 0) {
        for (auto i : item["data"]) {
            struct flow_now flow_now1{};
            flow_now1.id = i["id"];
            flow_now1.plan = i["plan"];
            flow_now1.plan_id = i["plan_id"];
            if (i["plan"] == 0) {
                flow_now1.pass = -2;
            } else {
                flow_now1.pass = i["pass"];
            }
            flow_now1.user_id = i["user_id"];
            flow_now1.note = i["note"];
            MAddFlowNow(flow_now1);
        }
    }
    res.set_status_and_content(status_type::ok);
}

static void DealFlow(request &req, response &res) {
    if (!check_login(req, res)) {
        return;
    }
    struct flow_now flow_now1{};
    flow_now1.id = atoi(req.get_query_value("id").data());
    flow_now1.plan = atoi(req.get_query_value("plan").data());
    flow_now1.plan_id = atoi(req.get_query_value("plan_id").data());
    flow_now1.user_id = atoi(req.get_query_value("user_id").data());
    flow_now1.pass = atoi(req.get_query_value("pass").data());
    flow_now1.note = req.get_query_value("note");
    MAddFlowNow(flow_now1);
    res.set_status_and_content(status_type::ok);
}

